package com.atguigu.spzx.manager.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.atguigu.spzx.common.exception.GuiguException;
import com.atguigu.spzx.common.log.annotation.Log;
import com.atguigu.spzx.manager.mapper.SysRoleUserMapper;
import com.atguigu.spzx.manager.mapper.SysUserMapper;
import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.model.dto.system.AssginRoleDto;
import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.dto.system.SysUserDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.LoginVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //用户登录
    @Override
    public LoginVo login(LoginDto loginDto) {
        //获取输入的验证码和存储到redis的key名称   LoginDto获取到
        String captcha = loginDto.getCaptcha();
        String key = loginDto.getCodeKey();

        // 2、根据获取的redis里面的key，查询redis里面的存储到验证码
        //set("user:validate"+key,
        String redisCode = redisTemplate.opsForValue().get("user:validate" + key);

        // 3、比较输入的验证码和redis存储的验证码是否一致
        if (StrUtil.isEmpty(redisCode) || !StrUtil.equalsIgnoreCase(redisCode,captcha)) {
            // 4、如果不一致，提示用户校验失败
            throw new GuiguException(ResultCodeEnum.VALIDATECODE_ERROR);
        }

        // 5、如果一致，删除redis里面的验证码
        redisTemplate.delete("user:validate" + key);

        // 1、获取提交用户名，loginDto获取到
        String userName = loginDto.getUserName();

        // 2、根据用户名查询数据库表 sys_user表
        SysUser sysUser = sysUserMapper.selectUserInfoByUserName(userName);

        // 3、如果根据用户名查询不到对应信息，用户不存在，返回错误信息
        if (sysUser == null){
//            throw new RuntimeException("用户名不存在！！！");
            throw new GuiguException(ResultCodeEnum.LOGIN_ERROR);
        }
        // 4、根据用户名查询到用户信息，用户存在
        // 5、获取输入的密码，比较输入的密码和数据库的密码是否一致
        String database_password = sysUser.getPassword();
        String input_password = DigestUtils.md5DigestAsHex(loginDto.getPassword().getBytes());
        //比较
        if (!input_password.equals(database_password)){
//            throw new RuntimeException("密码不正确！！！");
            throw new GuiguException(ResultCodeEnum.LOGIN_ERROR);
        }
        // 6、如果密码一致，登录成功，如果密码不一致登录失败
        // 7、登录成功，生成用户唯一标识token
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        // 8、把登录成功用户信息放到redis里面
        // key：token    values：用户信息
        redisTemplate.opsForValue()
                .set("user:login"+token, JSON.toJSONString(sysUser),7, TimeUnit.DAYS);
        // 9、返回loginvo对象
        LoginVo loginVo = new LoginVo();
        loginVo.setToken(token);
        return loginVo;
    }

    //获取当前登录用户信息
    @Override
    public SysUser getUserInfo(String token) {
        String userJson = redisTemplate.opsForValue().get("user:login" + token);
        SysUser sysUser = JSON.parseObject(userJson, SysUser.class);
        return sysUser;
    }

    //用户退出
    @Override
    public void logout(String token) {
        redisTemplate.delete("user:login"+token);
    }

    // 1 用户条件分页查询接口
    @Override
    public PageInfo<SysUser> findByPage(Integer pageNum, Integer pageSize, SysUserDto sysUserDto) {
        PageHelper.startPage(pageNum,pageSize);
        List<SysUser> list = sysUserMapper.findByPage(sysUserDto);
        PageInfo<SysUser> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    // 2 用户添加
    @Override
    public void saveSysUser(SysUser sysUser) {
        // 1、判断用户名不能重复
        String userName = sysUser.getUserName();
        SysUser dbSysUser = sysUserMapper.selectUserInfoByUserName(userName);
        if (dbSysUser != null){
            throw new GuiguException(ResultCodeEnum.USER_NAME_IS_EXISTS);
        }

        // 2、输入密码进行加密
        String md5_password = DigestUtils.md5DigestAsHex(sysUser.getPassword().getBytes());
        sysUser.setPassword(md5_password);

        // 设置status的值 1可用 0不可用
        sysUser.setStatus(1);

        sysUserMapper.save(sysUser);
    }

    // 3 用户修改
    @Override
    public void updateSysUser(SysUser sysUser) {
        sysUserMapper.update(sysUser);
    }

    // 4 用户删除
    @Override
    public void deleteById(Long userId) {
        sysUserMapper.delete(userId);
    }

    //用户分配角色
    @Log(title = "用户分配角色",businessType = 0)
    @Transactional
    @Override
    public void doAssign(AssginRoleDto assginRoleDto) {
        // 1、根据userId删除用户之前分配角色数据
        sysRoleUserMapper.deleteByUserId(assginRoleDto.getUserId());

        //为了测试模拟异常
//        int a = 1/0;

        // 2、重新分配数据
        List<Long> roleIdList = assginRoleDto.getRoleIdList();
        for (Long roleId : roleIdList) {
            //遍历得到灭个每个角色的id
            sysRoleUserMapper.doAssign(assginRoleDto.getUserId(),roleId);
        }
    }
}
